//
//  CircleController.swift
//  flowerpot
//
//  Created by fyname on 15/12/9.
//  Copyright © 2015年 senhe. All rights reserved.
//

import UIKit
import Starscream
import Alamofire
import SwiftyJSON



class SDChatUser : NSObject, SIMChatUserProtocol {
    init(identifier: String, name: String?, portrait: String?, gender: SIMChatUserGender) {
        self.name = name
        self.identifier = identifier
        self.portrait = portrait
        super.init()
    }
    
    /// 用户名
    var name: String?
    /// 用户头像
    var portrait: String?
    /// 用户唯一标识符
    var identifier: String = NSUUID().UUIDString
}



class CircleController: UITableViewController{
    
    var listData = NSMutableArray()
    
    var meId:String?, otherId:String?, meName:String?,otherName:String?
    
    let m = SDChatManager()
    //var flag:Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let flag = NSUserDefaults.standardUserDefaults().boolForKey("load")
        
        if(flag == false){
            
            let id = NSUserDefaults.standardUserDefaults().stringForKey("userId")
            let mn = NSUserDefaults.standardUserDefaults().stringForKey("userName")
            if( id != nil && mn != nil){
                Common.loadList(id!, callback: { (cb) -> Void in
                    self.listData = cb
                    self.tableView.reloadData()
                })
                meId = id
                meName = mn
            }else{
                print("用户未登录")
            }
            
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "load")
        }

        self.view.window?.backgroundColor = UIColor.whiteColor()
        
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.listData.count
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        let friendRelationsDO: FriendRelationsDO = self.listData.objectAtIndex(indexPath.row) as! FriendRelationsDO
        
        otherName = friendRelationsDO.passive_name
        
        otherId = friendRelationsDO.passive_id
        
        srand(UInt32(time(nil)))
        
        let me = SDChatUser(identifier: meId!, name: meName, portrait: nil, gender: .Male)
        let other = SDChatUser(identifier: otherId!, name: otherName, portrait: nil, gender: .Female)
        
        //
        m.login(me, finish: nil)
        // ;
        let cv = m.conversationWithRecver(other)
        let cc = SDChatViewController(conversation: cv)
        
        // 显示
        self.navigationController?.pushViewController(cc, animated: true)
        Common.mainTabBarController.tabBar.hidden = true
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let circleCell:String = "circleCell"
        
        //var cell = tableView.cellForRowAtIndexPath(indexPath)
        
        //if(cell == nil){
        let cell = tableView.dequeueReusableCellWithIdentifier(circleCell, forIndexPath: indexPath) as! CircleCell
        //}
        
        // Configure the cell...
        
        let friendRelationsDO: FriendRelationsDO = self.listData.objectAtIndex(indexPath.row) as! FriendRelationsDO
        
        //let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! IndexCell
        
        //let dataImg : NSData = NSData(contentsOfURL: NSURL(string : url)!)!
        //cell.logImage.image = UIImage(data: dataImg)
        //cell.logTitle.text = rowDict.objectForKey("video_title") as? String
        //cell.logTime.text = "2015-10-10 12:22"
        let pid = friendRelationsDO.passive_id
        let us = UserStatus.getUserStatus(pid!)

        
        if(us.userStatus == "1"){
            
            print("us:"+us.userId!)
            print("us:"+us.userStatus!)
            cell.userImage = UIImageView(image: UIImage(named: "weixin"))
            cell.userName.text = friendRelationsDO.passive_name
            cell.lasTime.text = "刚刚"
            cell.lastMsg.text = "今天天气不错哦!"
            
        }else {
            cell.userImage = UIImageView(image: UIImage(named: "weixin"))
            cell.userName.text = friendRelationsDO.passive_name
            cell.lasTime.text = "30分钟前"
            cell.lastMsg.text = "今天天气不错哦!"
        }
        

        return cell
    }

}
